﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 共享单车管理系统
{
    public partial class Uptaer_kh : Form
    {
        public Uptaer_kh()
        {
            InitializeComponent();
        }
        SqlDataAdapter daClient, dalog;
        DataSet ds = new DataSet();
        void showAll()
        {
            DataView dvclient = new DataView(ds.Tables["client_info"]);
            dgvclient.DataSource = dvclient;

        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (txtname.Text == "")
            {
                MessageBox.Show("用户名不能为空");
            }
            else
            {
                DialogResult dr = MessageBox.Show("你确定要修改吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    int a = dgvclient.CurrentRow.Index;
                    string str = dgvclient.Rows[a].Cells["UserID"].Value.ToString();
                    DataRow[] CustRow = ds.Tables["client_info"].Select("UserID='" + str + "'");
                    CustRow[0]["Username"] = txtname.Text;
                
                    CustRow[0]["Phone"] = txtipone.Text;
                    CustRow[0]["Email"] = txtemail.Text;
                 
                    MessageBox.Show("修改成功");

                    DataRow drlog = ds.Tables["log_info"].NewRow();

                    drlog["username"] = Login.username;
                    drlog["Type"] = "修改";
                    drlog["Time"] = DateTime.Now;
                    drlog["table"] = "users表";
                    ds.Tables["log_info"].Rows.Add(drlog);

                    UpdateDB();
                }
                else
                {
                    return;
                }
            }
        }
        void UpdateDB()
        {
            SqlTransaction st = tansactionInit();
            try
            {
                daClient.Update(ds, "client_info");
                dalog.Update(ds, "log_info");
                st.Commit();
                DB.cn.Close();
            }
            catch (SqlException ex)
            {
                st.Rollback();
                DB.cn.Close();
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void Uptaer_kh_Load(object sender, EventArgs e)
        {
            init();
            showAll();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvclient_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtid.Text = dgvclient.CurrentRow.Cells["UserID"].Value.ToString();

            txtname.Text = dgvclient.CurrentRow.Cells["Username"].Value.ToString();
         
            txtipone.Text = dgvclient.CurrentRow.Cells["Phone"].Value.ToString();
            txtemail.Text = dgvclient.CurrentRow.Cells["Email"].Value.ToString();
            
        }
        private SqlTransaction tansactionInit()
        {
            DB.GetCn();
            SqlCommandBuilder dbclient = new SqlCommandBuilder(daClient);
            dbclient.DataAdapter.UpdateCommand = dbclient.GetUpdateCommand(); // 指定要更新的表名
            SqlCommandBuilder dblog = new SqlCommandBuilder(dalog);
            dblog.DataAdapter.InsertCommand = dblog.GetInsertCommand(); // 指定要更新的表名

            
            SqlTransaction st = DB.cn.BeginTransaction();
            daClient.UpdateCommand.Transaction = st;
            dalog.InsertCommand.Transaction = st;

            daClient.UpdateCommand.Connection = DB.cn;
            dalog.InsertCommand.Connection = DB.cn;

            return st;
        }
        void init()
        {
            DB.GetCn();
            string str = "select * from Users";
            daClient = new SqlDataAdapter(str, DB.cn);
            string sdr = "select * from log";
            dalog = new SqlDataAdapter(sdr, DB.cn);
            daClient.Fill(ds, "client_info");
            dalog.Fill(ds, "log_info");
        }



        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
